package com.atguigu.gmall.realtime.util

import com.alibaba.fastjson.serializer.SerializeConfig
import com.alibaba.fastjson.{JSON, JSONObject}
import com.atguigu.gmall.realtime.bean.PageInfo

object JsonTest {

  def main(args: Array[String]): Unit = {
    //把json转成通用对象
    val json=
        s"""
           |{"common":{"ar":"230000","ba":"vivo","ch":"xiaomi","is_new":"0","md":"vivo iqoo3","mid":"mid_171","os":"Android 11.0","uid":"25","vc":"v2.1.134"},"page":{"during_time":2273,"last_page_id":"mine","page_id":"orders_unpaid"},"ts":1635320941000}
         """.stripMargin

      val jSONObject: JSONObject = JSON.parseObject(json)
      val pageObj: JSONObject = jSONObject.getJSONObject("page")
      println(pageObj.getString("last_page_id"))
   // 把通用对象转成json
    println(JSON.toJSONString(pageObj,new SerializeConfig(true)))
   //把json字符串转成专用对象
      val jsonString: String = pageObj.toJSONString
    val pageInfo: PageInfo = JSON.parseObject(jsonString,classOf[PageInfo])
    println(pageInfo)
    //把专用对象转为json字符串
    println(JSON.toJSONString(pageInfo, new SerializeConfig(true)))

    //json工具 fastjson(alibaba)  gson（google）  jackson（apache）
  }

}
